package com.horay.springbootdemo.data.source.multiple.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 数据源配置
 *
 * @author heyingcheng
 * @date 2019/10/31 21:31
 */
@Slf4j
@Configuration
public class BarDataSourceConfiguration {

    @Bean
    @ConfigurationProperties("spring.bar.datasource")
    public DataSourceProperties barDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource barDataSource(DataSourceProperties barDataSourceProperties) {
        log.info("bar.datasource.url:[{}]", barDataSourceProperties.getUrl());
        return barDataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    public JdbcTemplate barJdbcTemplate(DataSource barDataSource) {
        return new JdbcTemplate(barDataSource);
    }

    @Bean
    public PlatformTransactionManager barTransactionManager(DataSource barDataSource) throws Exception {
        return new DataSourceTransactionManager(barDataSource);
    }

}
